package com.app.gridview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.zndroid.gridview.load.IImageDisplayEngine;
import com.zndroid.gridview.load.ILoadCallBack;

import java.io.File;

public class GlideImageDisplay implements IImageDisplayEngine {
    @Override
    public boolean isOpenInnerProgress() {
        return false;
    }

    @Override
    public ILoadCallBack getLoadCallBack() {
        return null;
    }

    //择其一实现即可，虽然，内部做了优先级策略，但是还是建议选择所需实现，至少代码少写了不是
    @Override
    public void onDisplayImage(Context context, ImageView imageView, String url, @Nullable ILoadCallBack loadCallBack) {
        if (isOpenInnerProgress()) {
            Glide.with(context)
                    .load(url)
                    .placeholder(R.drawable.ic_gil_default_color)
                    .error(R.drawable.ic_gil_default_color)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imageView);
        } else {
            Glide.with(context)
                    .load(url)
                    .placeholder(R.drawable.ic_gil_default_color)
                    .error(R.drawable.ic_gil_default_color)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            if (null != loadCallBack) {
                                loadCallBack.onLoadFailed();
                            }
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            if (null != loadCallBack) {
                                loadCallBack.onLoadSuccess();
                            }
                            return false;
                        }
                    })
                    .into(imageView);

        }
    }

    @Override
    public void onDisplayImage(Context context, ImageView imageView, int resId, @Nullable ILoadCallBack loadCallBack) {
        Glide.with(context)
                .load(resId)
                .placeholder(R.drawable.ic_gil_default_color)
                .error(R.drawable.ic_gil_default_color)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    @Override
    public void onDisplayImage(Context context, ImageView imageView, @NonNull File file, @Nullable ILoadCallBack loadCallBack) {
        Glide.with(context)
                .load(file)
                .placeholder(R.drawable.ic_gil_default_color)
                .error(R.drawable.ic_gil_default_color)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);

        if (loadCallBack != null) {
            loadCallBack.onLoadSuccess();
        }
    }

    @Override
    public Bitmap getCacheImage(String url) {
        return null;
    }
}
